package com.sidneysimmons.financially.activities;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Vibrator;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.TextView;

import com.sidneysimmons.financially.R;

public class BaseActivity extends FragmentActivity {
	
	private final String CLASS_NAME = this.getClass().getName();
	
	// Constants
	protected final int DIALOG_ERROR = 6;
	protected final int DIALOG_PROGRESS = 14;
	
	// System services
	private InputMethodManager inputManager;
	private Vibrator vibrator;
	
	protected void onCreate(Bundle savedInstanceState, String className) {
		// Log which Activity onCreate() was called
		Log.i(CLASS_NAME, "onCreate() - " + className);
		super.onCreate(savedInstanceState);
		
		// Get the system services, etc
		inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
		vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
	}
	
	protected void onStart(String className) {
		// Log which Activity onStart() was called
		Log.i(CLASS_NAME, "onStart() - " + className);
		
		super.onStart();
	}
	
	protected void onResume(String className) {
		// Log which Activity onResume() was called
		Log.i(CLASS_NAME, "onResume() - " + className);
		
		super.onResume();
	}
	
	protected void onPause(String className) {
		// Log which Activity onPause() was called
		Log.i(CLASS_NAME, "onPause() - " + className);
		
		super.onPause();
	}
	
	protected void onStop(String className) {
		// Log which Activity onStop() was called
		Log.i(CLASS_NAME, "onStop() - " + className);
		
		super.onStop();
	}
	
	protected void onDestroy(String className) {
		// Log which Activity onDestroy() was called
		Log.i(CLASS_NAME, "onDestroy() - " + className);
		
		super.onDestroy();
	}
	
	/**
	 * Attempt to clear the soft keyboard from view.
	 * 
	 * @param viewBinder - The window token from one of the EditText views. 
	 * This can be retrieved with <code>getWindowToken()</code>.
	 */
	protected void closeSoftKeyboard(IBinder viewBinder) {
		if (inputManager != null) {
			inputManager.hideSoftInputFromWindow(viewBinder, 0);
		}
	}
	
	/**
	 * Called when a dialog is first created.  Set up any listeners and initial 
	 * values in this method.  This method is usually only called once (when first 
	 * created).<br><br>
	 * 
	 * Use <code>onPrepareDialog()</code> to set text and other 
	 * values that can change.  That method is called when showing the dialog again 
	 * after it has already been created.
	 * 
	 * @param id - The id of the type of dialog to display.
	 * @param args - A bundle containing any data used in the dialog.
	 */
	@Override
	protected Dialog onCreateDialog(int id, Bundle args) {
		if (id == DIALOG_PROGRESS) {
			ProgressDialog progressDialog = new ProgressDialog(this);
			progressDialog.setTitle("Such Title");
			progressDialog.setMessage("Very Message");
			progressDialog.show();
			return progressDialog;
		} else if (id == DIALOG_ERROR) {
			AlertDialog.Builder alertBuilder = new AlertDialog.Builder(this);
			alertBuilder.setMessage("Such message");
			alertBuilder.setPositiveButton("Okay", new OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					removeDialog(DIALOG_ERROR);
				}
			});
			alertBuilder.setCancelable(false);
			AlertDialog errorDialog = (AlertDialog) alertBuilder.create();
			errorDialog.show();
			return errorDialog;
		}
		
		return null;
	}
	
	/**
	 * Called after <code>onCreateDialog()</code> to set any dynamic text 
	 * or other values that might change.  This method is called any time 
	 * the dialog is shown.
	 * 
	 * @param id - The id of the type of dialog to display.
	 * @param dialog - The dialog about to be shown.
	 * @param args - A bundle containing any data used in the dialog.
	 */
	@Override
	protected void onPrepareDialog(int id, Dialog dialog, Bundle args) {
		super.onPrepareDialog(id, dialog, args);
		
		if (id == DIALOG_PROGRESS) {
			String title = args.getString("title");
			String message = args.getString("message");
			
			ProgressDialog progressDialog = (ProgressDialog) dialog;
			progressDialog.setTitle(title);
			progressDialog.setMessage(message);
			progressDialog.setIndeterminate(true);
			progressDialog.setCancelable(false);
		} else if (id == DIALOG_ERROR) {
			String message = args.getString("message");
			
			AlertDialog alertDialog = (AlertDialog) dialog;
			alertDialog.setMessage(message);
		}
	}
	
	/**
	 * Vibrates the device for the specified number of milliseconds.
	 * 
	 * @param milliseconds - The number of milliseconds to vibrate.
	 */
	protected void vibrate(long milliseconds) {
		if (vibrator != null) {
			vibrator.vibrate(milliseconds);
		}
	}
	
	/**
	 * Attached to all text links.  This listens for up/down touches and changes the 
	 * color of the link when it's being pressed.
	 */
	protected OnTouchListener linkTouchListener = new OnTouchListener() {
		@Override
		public boolean onTouch(View v, MotionEvent event) {
			TextView tv = (TextView) v;
			if (event.getAction() == MotionEvent.ACTION_DOWN) {
				int linkDepressed = getResources().getColor(R.color.link_depressed);
				tv.setTextColor(linkDepressed);
			} else if (event.getAction() == MotionEvent.ACTION_UP) {
				int link = getResources().getColor(R.color.link);
				tv.setTextColor(link);
			}
			return false;
		}
	};
}